package com.gistone.commis.api.restController;

import com.gistone.commis.dto.EditorResult;
import com.gistone.commis.dto.FieldError;
import com.gistone.commis.dto.Option;
import com.gistone.commis.dto.Options;
import com.gistone.commis.entity.*;
import com.gistone.commis.mapper.*;
import com.gistone.commis.util.AuditState;
import com.gistone.commis.util.DataTablePageUtil;
import com.gistone.commis.util.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController("apiPropertyLogController")
@RequestMapping("api/propertyLog")
public class PropertyLogController {

	private static final Logger logger = LoggerFactory.getLogger(PropertyLogController.class);



	@Autowired
	private PropertyTypeMapper propertyTypeMapper;

	@Autowired
	private PropertyLogMapper propertyLogMapper;

	@Autowired
	private CommunityMapper communityMapper;

	@Autowired
	private DepartMapper departMapper;





	/**
	 * dataTables 表单数据返回
	 *
	 * @param request
	 * @return
	 */

	@Transactional
	@RequestMapping(method = RequestMethod.GET, value = "/pageDatatables")
	public DataTablePageUtil<PropertyLog>  pageDatatables(HttpServletRequest request, PropertyLog propertyLog) {
//		HttpSession httpSession = request.getSession();
//		User user = (User) httpSession.getAttribute("userInfo");
//		logger.info("用户信息：" + user.toString());
//		int community_id = user.getCommunity().getCommunityId();
//		propertyLog.setCommunityId(community_id);

		List<Community> communities =communityMapper.findCommunitys();
		Options os =new Options();
		List<Option> optionList =new ArrayList<>();
		for (Community community : communities) {

			Option option =new Option();
			option.setLabel(community.getCommunityName());
			option.setValue(community.getCommunityId());
			optionList.add(option);


		}
		os.setCommunityId(optionList);
		List<Depart> departs =departMapper.findDeparts();
		optionList =new ArrayList<>();
		for (Depart depart : departs) {

			Option option =new Option();
			option.setLabel(depart.getDepartName());
			option.setValue(depart.getDepartId());
			optionList.add(option);


		}

		os.setDepartId(optionList);

		List<PropertyType> propertyTypes =propertyTypeMapper.findPropertyTypes();
		optionList =new ArrayList<>();
		for (PropertyType propertyType : propertyTypes) {

			Option option =new Option();
			option.setLabel(propertyType.getTypeName());
			option.setValue(propertyType.getTypeId());
			optionList.add(option);


		}
		os.setTypeId(optionList);

		DataTablePageUtil<PropertyLog> dataTable = new DataTablePageUtil<>(request);

		PageHelper.startPage(dataTable.getPage_num(), 10);
		List<PropertyLog> list = propertyLogMapper.selectSelective(propertyLog);
		PageInfo<PropertyLog> pageInfo =new PageInfo<>(list);
		System.out.println(list.size());
		dataTable.setDraw(dataTable.getDraw());
		dataTable.setData(pageInfo.getList());
		dataTable.setOptions(os);
		dataTable.setRecordsTotal((int)pageInfo.getTotal());
		dataTable.setRecordsFiltered(dataTable.getRecordsTotal());
		//返回数据到页面

		return dataTable;

	}







}
